var reg = /id=([^&]+)/
var arr = location.search.match(reg)
var goodsId = getCookie('mashuaishuai')
console.log(getCookie('mashuaishuai'));

console.log(arr);
if(arr && arr[1]){
    // 获取到商品id
    goodsId = arr[1]
    // 发送商品详情的请求
    // getData()
}
$.ajax({
    url:"http://localhost:8888/goods/item",
    type:"get",
    data:{
        id:goodsId
    },
    success:function(res){
        console.log(res);
        if(res.code === 1){
            // 动态渲染数据
            // 放大镜图片渲染
            $('.zoom .middle>img').attr('src', res.info.img_big_logo)
            var imgHTML = ''
            for(var i=0;i<$('.zoom .small img').length;i++){
                imgHTML += `
                    <li class="active"><img src="${res.info.img_big_logo}" alt=""></li>
                `
            }
            $('.zoom .small').html(imgHTML)
            $('.zoom .middle .big img').attr('src', res.info.img_big_logo)
            // ajax数据渲染完成以后再开启放大镜效果
            var t = new Zoom()
            t.move()

            // 渲染商品数据
            $('.goods_case_tit').text(res.info.title)
            $('.currentprice').text(res.info.current_price)
            $('.goods_extend_content').children().eq(0).html(res.info.goods_introduce)

            // 加入购物车
            addCart()
        }
        
    }
})
function getData(){
    $.get('http://localhost:8888/goods/item?id='+goodsId).then(res => {
        console.log(res);
        if(res.code === 1){
            // 动态渲染数据
            // 放大镜图片渲染
            $('.zoom .middle>img').attr('src', res.info.img_big_logo)
            var imgHTML = ''
            for(var i=0;i<$('.zoom .small img').length;i++){
                imgHTML += `
                    <li class="active"><img src="${res.info.img_big_logo}" alt=""></li>
                `
            }
            $('.zoom .small').html(imgHTML)
            $('.zoom .middle .big img').attr('src', res.info.img_big_logo)
            // ajax数据渲染完成以后再开启放大镜效果
            var t = new Zoom()
            t.move()

            // 渲染商品数据
            $('.goods_case_tit').text(res.info.title)
            $('.currentprice').text(res.info.current_price)
            $('.goods_extend_content').children().eq(0).html(res.info.goods_introduce)

            // 加入购物车
            addCart()
        }else{
            layer.msg('非法访问！', {
                icon: 2,
                time: 2000
            }, function(){
                location.href = 'list.html'
            })
        }
    })
}
function addCart(){
    // 点击加入购物车按钮
    $('.addCart').click(function(){
        // 判断用户是否登录
        if(!username){
            layer.msg('请先登录！', {
                icon: 2,
                time: 2000
            }, function(){
                // 强制跳转到登录页面
                location.href = 'login.html?callback='+location.href
            })
        }else{
            // 已经登录了
            // 发送请求添加购物车
            $.ajax({
                url: 'http://localhost:8888/cart/add',
                method: 'post',
                data: {
                    goodsId,
                    id: getCookie('userId')
                },
                headers: {
                    'authorization': localStorage.getItem('token') || ''
                }
            }).then(res => {
                // console.log(res);
                if(res.code === 1){
                    layer.msg(res.message, {
                        icon: 1,
                        time: 2000
                    })
                }else{
                    layer.msg('加入购物车失败，请刷新重试！', {
                        icon: 2,
                        time: 2000
                    })
                }
            })
        }
    })
    
}